package com.liudadan.aspect_03;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.Arrays;


@Aspect
@Component
public class LogAspect {

    //1.进入通知 @Before
    @Before("execution(* com..aop_02.impl.*.*(..))")
    public void begin(JoinPoint joinPoint){
        Object target = joinPoint.getTarget(); //获取目标对象

        String name = joinPoint.getClass().getName();

        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("类："+name + " 方法：" + methodName + " 传入参数：" + Arrays.toString(args) + " 开始执行~~~");

    }

    //2.结束通知 @AfterReturning
    @AfterReturning(value = "execution(* com..aop_02.impl.*.*(..))",returning = "result")
    public void afterReturning(JoinPoint joinPoint,Object result){
        String name = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println("类："+name + " 方法：" + methodName + " 正常执行完毕了~~~ 结果为：" + result);

    }

    //3.报错通知 @AfterThrowing
    @AfterThrowing(value = "execution(* com..aop_02.impl.*.*(..))",throwing = "t")
    public void afterThrowing(JoinPoint joinPoint,Throwable t){
        String name = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println("类："+name + " 方法：" + methodName + " 报错了~~~ 错误为：" + t.getMessage());
    }

}
